import re
import datetime


class TimeConverter:
    def __init__(self):
        self._ch2integer = {
            '0': ['〇', '零'],
            '1': ['一', '壹'],
            '2': ['二', '貳', '贰'],
            '3': ['三', '叁'],
            '4': ['四', '肆'],
            '5': ['五', '伍'],
            '6': ['六', '陆'],
            '7': ['七', '柒'],
            '8': ['八', '捌'],
            '9': ['九', '玖'],
        }
        self.year_pattern = "([12][0-9]{3})"
        self.month_pattern = "(([0?][1-9])|([1?][0-2])|[1-9])"
        self.day_pattern = "(1[0-9]|2[0-9]|3[0-1]|0?[1-9])"
        self._default_date = "9999-12-31"

    def convert_ch2integer(self, candidate_text):
        valid_str = ""
        candidate_text = re.sub("拾", "十", candidate_text)
        candidate_text = re.sub("廿", "二十", candidate_text)
        for _char in candidate_text:
            state = False
            for key, val in self._ch2integer.items():
                if _char not in val:
                    continue
                valid_str += key
                state = True
                break
            if not state:
                valid_str += _char

        valid_str = re.sub("([\\d])十([\\d])", "\\1" + "\\2", valid_str)
        valid_str = re.sub("([^\\d])十([^\\d])", "\\1" + " 10\\2", valid_str)
        valid_str = re.sub("([\\d])十([^\\d])", "\\1" + " 0\\2", valid_str)
        valid_str = re.sub("([^\\d])十([\\d])", "\\1" + " 1\\2", valid_str)
        valid_str = re.sub("\\s", "", valid_str)
        return valid_str

    def lower_case_date(self, candidate_text):
        year, month, day = "", "", ""

        y_search = re.search("{}[\\u4E00-\\u9FA5]?".format(self.year_pattern), candidate_text)
        if not y_search:
            m_search = re.search("{}月".format(self.month_pattern), candidate_text)
            if m_search:
                month = m_search.group(1)
            d_search = re.search("{}日".format(self.day_pattern), candidate_text)
            if d_search:
                day = d_search.group(1)
        else:
            year = y_search.group(1)
            # print(y_search)
            begin_id = y_search.span()[1]
            m_search = re.search("^{}[^\\d日]?".format(self.month_pattern), candidate_text[begin_id:])
            # print(m_search)
            if m_search:
                begin_id = m_search.span()[1] + begin_id
                month = m_search.group(1)
            d_search = re.search("^{}[^\\d月]?".format(self.day_pattern), candidate_text[begin_id:])
            # print(d_search)
            if d_search:
                day = d_search.group(1)

        format_date = "-".join((year + " " + month + " " + day).strip().split(" "))
        return format_date

    def convert_date(self, candidate_text):
        if candidate_text == "长期":
            return self._default_date

        if not re.search("[年月日\\d{4}]", candidate_text) or len(candidate_text) >= 30 or len(candidate_text) <= 1:
            return ""

        if re.search("[〇零一壹二貳贰]", candidate_text):
            candidate_text = self.convert_ch2integer(candidate_text)
            return self.lower_case_date(candidate_text)

        if re.search("[0-9]", candidate_text):
            return self.lower_case_date(candidate_text)
        return ""

    def convert_validity_date(self, str_term):
        term_lst = str_term.split("至")
        if len(term_lst) == 2:
            return self.convert_date(term_lst[1])

        return self.convert_date(term_lst[0])


if __name__ == '__main__':
    converter = TimeConverter()
    strCNTime = '有效期限:至二0二0年网月日'
    standTime = converter.convert_validity_date(strCNTime)
    print(standTime)
